$(function(){
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dataGrid: null,
        params: {
            type: false,
            typeEnum: false
        },
        dom: {
            depList: null
        },
        init: function () {
            var $this = this;
            this.params.type = xhTool.getQuery('type');

            this.dom.depList = $('#depList');
            this.dom.depList.treegrid({
                url: '/group/getDepGroup',
                queryParams: {
                    type: $this.params.type
                },
                // onLoadSuccess:function() {
                //     $this.dom.depList.treegrid('collapseAll');
                // }
            });

            this.dataGrid = new xhDataGrid($('#list'), 'Group/getGroup', {type: this.params.type});
            this.dataGrid.parseToolbar().init()
                .activeBtn($('#bt_list_move, #bt_list_del, #bt_list_reset'), $('#bt_list_set_power, #bt_list_user, #bt_list_edit'));

            this.listener();
            $this.funObj().getTypeEnum();
        },
        listener: function () {
            var $this = this;
            $this.dataGrid.onLoadSuccess = function (data) {
                $this.dataGrid.clear();
            };
            //添加
            $('#bt_list_add').click(function() {
                $this.funObj().addGroup();
            });
            //设置权限
            $('#bt_list_set_power').click(function() {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().openGroupPower();
            });
            //查看成员
            $('#bt_list_user').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().viewGroupUser();
            });
            //查询
            xhTool.bindDocKeyDown($('#bt_list_search').click(function () {
                $this.funObj().search();
            }));
            //移动至部门
            $('#bt_list_move').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().moveDep();
            });
            //删除
            $('#bt_list_del').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delGroup();
            });
            //编辑
            $('#bt_list_edit').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().editGroup();
            });
            //移动至部门
            $('#bt_list_reset').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().resetGroupDep();
            });
        },
        funObj: function(){
            var $this = this;
            return {
                addGroup: function() {
                    // if (!$this.params.typeEnum) {
                    //     $this.funObj().getTypeEnum();
                    //     return false;
                    // }
                    var htmlDom = '<input id="group_name" class="swal2-input" placeholder="组名称" />';
                    if (!$this.params.type) {
                        var typeDom = $('<select id="group_type" class="swal2-select"></select>');
                        $.each($this.params.typeEnum, function (value, text) {
                            typeDom.append('<option value="' + value + '">' + text + '</option>')
                        });
                        htmlDom += typeDom[0].outerHTML;
                    }
                    window.top.swal({
                        title: '新建组',
                        // input: 'text',
                        // inputPlaceholder: '组名称',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        html: htmlDom,
                        preConfirm: function () {
                            return new Promise(function (resolve, reject) {
                                var groupName = $('#group_name', window.top.document).val(),
                                    groupType = $('#group_type', window.top.document).val();
                                if (!groupName) {
                                    reject('组名称非法！');
                                } else {
                                    xhExtra.ajaxRequest('Group/createGroup', {name: groupName, type: $this.params.type || groupType}, {success: true, table: $this.dataGrid});
                                }
                            })
                        }
                    }).then(function (name) {
                    }, function(dismiss) {});
                },
                openGroupPower: function() {
                    window.top.xhTop.createNewTab('设置组权限', baseUrl + 'Group/groupPower?groupId=' + $this.dataGrid.getSelectValue('iGroupId'), true);
                },
                getTypeEnum: function () {
                    xhExtra.ajaxRequest('Group/getGroupType', {}, {check: true, success: false}, function(data) {
                        $this.params.typeEnum = data;
                        // $this.funObj().addGroup.call(this);
                    });
                },
                viewGroupUser: function () {
                    window.top.xhTop.showModal({
                        title: $this.dataGrid.getSelectValue('sName') + ' - 成员',
                        content: xhTool.getFrameString('group/groupUser', {groupId: $this.dataGrid.getSelectValue('iGroupId')}, 600),
                    });
                },
                search: function () {
                    $this.dataGrid.reload({
                        type: xhExtra.getValue($('#list_type')),
                        search:{
                            vague: xhExtra.getValue($('#list_vague'))
                        }
                    });
                },
                moveDep: function () {
                    var groupIds = $this.dataGrid.getSelectValues('iGroupId'),
                        depRow   = $this.dom.depList.treegrid('getSelected'),
                        depId    = depRow ? depRow.iDepId : 0;
                    if (!groupIds) {
                        window.top.xhTop.showError('请选择权限组');
                        return false;
                    }
                    if (!depId) {
                        window.top.xhTop.showError('请选择部门');
                        return false;
                    }
                    xhExtra.askChangeStatus('group/setGroupDep', {groupIds: groupIds, depId: depId}, null, '确认转移？', function (result) {
                        $this.dom.depList.treegrid('load');
                    });
                },
                delGroup: function () {
                    var groupIds = $this.dataGrid.getSelectValues('iGroupId');
                    xhExtra.askChangeStatus('group/delGroup', {groupIds: groupIds}, $this.dataGrid, '确认删除？');
                },
                resetGroupDep: function () {
                    var groupIds = $this.dataGrid.getSelectValues('iGroupId');
                    xhExtra.askChangeStatus('group/resetGroupDep', {groupIds: groupIds}, $this.dataGrid, '确认重置？', function (result) {
                        $this.dom.depList.treegrid('load');
                    });
                },
                editGroup: function () {
                    // if (!$this.params.typeEnum) {
                    //     $this.funObj().getTypeEnum();
                    //     return false;
                    // }
                    var groupId = $this.dataGrid.getSelectValue('iGroupId'),
                        groupName = $this.dataGrid.getSelectValue('sName'),
                        groupType = $this.dataGrid.getSelectValue('iType');
                    var htmlDom = '<input id="group_name" class="swal2-input" placeholder="组名称" value="' + groupName + '" />';
                    if (!$this.params.type) {
                        var typeDom = $('<select id="group_type" class="swal2-select"></select>');
                        $.each($this.params.typeEnum, function (value, text) {
                            if (text === groupType) {
                                typeDom.append('<option value="' + value + '" selected="selected">' + text + '</option>')
                            }
                            else {
                                typeDom.append('<option value="' + value + '">' + text + '</option>')
                            }
                        });
                        htmlDom += typeDom[0].outerHTML;
                    }
                    window.top.swal({
                        title: '编辑组',
                        // input: 'text',
                        // inputPlaceholder: '组名称',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        html: htmlDom,
                        preConfirm: function () {
                            return new Promise(function (resolve, reject) {
                                var groupName = $('#group_name', window.top.document).val(),
                                    groupType = $('#group_type', window.top.document).val();
                                if (!groupName) {
                                    reject('组名称非法！');
                                } else {
                                    xhExtra.ajaxRequest(
                                        'Group/modifyGroup',
                                        {groupId: groupId, name: groupName, type: $this.params.type || groupType},
                                        {success: true, table: $this.dataGrid}
                                    );
                                }
                            })
                        }
                    }).then(function (name) {
                    }, function(dismiss) {});
                }
            }
        }
    }
};